package com.fsds.crem.web.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fsds.crem.core.constant.Globals;
import com.fsds.crem.web.dao.NodeInfoDao;
import com.fsds.crem.web.dao.StatisticReportDao;
import com.fsds.crem.web.entity.StatisticReport;
import com.fsds.crem.web.service.StatisticReportService;

@Service("statisticReportService")
public class StatisticReportServiceImpl implements StatisticReportService {

	@Resource
	private StatisticReportDao statisticReportDao;
	@Resource
	private NodeInfoDao nodeInfoDao;
	
	@Override
	public Map<String, Object> findTotalSaleEarningByDay() {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 日期集合
		List<String> dateList = getDateList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		// 总数
		List<StatisticReport> totalList = statisticReportDao.findSaleEarningByDay(statisticReport);
		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		map.put("dateList", dateList);
		map.put("totalList", repairLackDataForDay(dateList, totalList));
		map.put("pizhouList", repairLackDataForDay(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForDay(dateList, fuyangList));
		map.put("hushiList", repairLackDataForDay(dateList, hushiList));
		map.put("weekDetailList", new ArrayList<String>());
		
		return map;
	}

	@Override
	public Map<String, Object> findAreaSaleEarningByDay() {

		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 日期集合
		List<String> dateList = getDateList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		
		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		map.put("dateList", dateList);
		map.put("pizhouList", repairLackDataForDay(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForDay(dateList, fuyangList));
		map.put("hushiList", repairLackDataForDay(dateList, hushiList));
		map.put("weekDetailList", new ArrayList<String>());
		
		return map;
	}

	@Override
	public Map<String, Object> findOneNodeSaleEarningByDay(StatisticReport statisticReport) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 日期集合
		List<String> dateList = getDateList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		// 某一门店的数据
		List<StatisticReport> list = statisticReportDao.findSaleEarningByDay(statisticReport);
		
		map.put("dateList", dateList);
		map.put("list", repairLackDataForDay(dateList, list));
		map.put("nodeInfo", nodeInfoDao.findNodeByCode(statisticReport.getNodeCode()));
		map.put("weekDetailList", new ArrayList<String>());
		
		return map;
	}
	
	/**
	 * 根据开始和结束日期获取时间段中所有的日期的集合
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<String> getDateList(Date begin,Date end){
		
		Calendar c = Calendar.getInstance();
		c.setTime(begin);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		
		List<String> list = new ArrayList<String>();
		
		while(c.getTime().before(end)){
			
			list.add(sdf.format(c.getTime()));
			
			c.add(Calendar.DAY_OF_MONTH, 1);
		}	
		
		return list;
	}

	/**
	 * 用0补全没有的数据
	 * 
	 * @param dateList
	 * @param list
	 * @return
	 */
	public List<Float> repairLackDataForDay(List<String> dateList,List<StatisticReport> list){
		
		List<Float> dataList = new ArrayList<Float>();
		
		// 数据完整，不用补全
		if(dateList.size() == list.size()){
			
			for(StatisticReport statisticReport : list){
				
				dataList.add(statisticReport.getSaleEarning());
			}
		}
		// 数据不全，用0补全
		else{
			
			Map<String, Float> dataMap = new HashMap<String, Float>();
			
			for(StatisticReport statisticReport : list){
				dataMap.put(statisticReport.getCreateDate(), statisticReport.getSaleEarning());
			}
			
			for(String str : dateList){
				
				if(dataMap.containsKey(str)){
					dataList.add(dataMap.get(str));
				}
				else{
					dataList.add(0f);
				}
			}
		}
		
		return dataList;
	}
	

	@Override
	public Map<String, Object> findTotalSaleEarningByWeek() {

		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getWeekList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		// 总数
		List<StatisticReport> totalList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		map.put("dateList", dateList);
		map.put("totalList", repairLackDataForWeek(dateList, totalList));
		map.put("pizhouList", repairLackDataForWeek(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForWeek(dateList, fuyangList));
		map.put("hushiList", repairLackDataForWeek(dateList, hushiList));
		map.put("weekDetailList", getDetailForWeek(dateList));
		
		return map;
		
	}

	@Override
	public Map<String, Object> findAreaSaleEarningByWeek() {

		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getWeekList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());

		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		map.put("dateList", dateList);
		map.put("pizhouList", repairLackDataForWeek(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForWeek(dateList, fuyangList));
		map.put("hushiList", repairLackDataForWeek(dateList, hushiList));
		map.put("weekDetailList", getDetailForWeek(dateList));

		return map;
	}

	@Override
	public Map<String, Object> findOneNodeSaleEarningByWeek(
			StatisticReport statisticReport) {

		Map<String, Object> map = new HashMap<String, Object>();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getWeekList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		// 某一门店的数据
		List<StatisticReport> list = statisticReportDao.findSaleEarningByWeek(statisticReport);
		
		map.put("dateList", dateList);
		map.put("list", repairLackDataForWeek(dateList, list));
		map.put("nodeInfo", nodeInfoDao.findNodeByCode(statisticReport.getNodeCode()));
		map.put("weekDetailList", getDetailForWeek(dateList));
		
		return map;
	}
	
	/**
	 * 根据开始和结束日期获取时间段中所有的日期的集合
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<String> getWeekList(Date begin,Date end){
		
		Calendar c = Calendar.getInstance();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(begin);
		
		int currentDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		
		if(currentDayOfWeek != Calendar.MONDAY){
			c.add(Calendar.WEEK_OF_YEAR, 1);
		}
		
		List<String> list = new ArrayList<String>();
		
		while(c.getTime().before(end)){
			
			list.add(c.get(Calendar.YEAR) + "" + c.get(Calendar.WEEK_OF_YEAR));
			
			c.add(Calendar.WEEK_OF_YEAR, 1);
		}	
		
		return list;
	}
	
	/**
	 * 用0补全没有的数据
	 * 
	 * @param dateList
	 * @param list
	 * @return
	 */
	public List<Float> repairLackDataForWeek(List<String> weekList,List<StatisticReport> list){
		
		List<Float> dataList = new ArrayList<Float>();
		
		// 数据完整，不用补全
		if(weekList.size() == list.size()){
			
			for(StatisticReport statisticReport : list){
				
				dataList.add(statisticReport.getSaleEarning());
			}
		}
		// 数据不全，用0补全
		else{
			
			Map<String, Float> dataMap = new HashMap<String, Float>();
			
			for(StatisticReport statisticReport : list){
				dataMap.put(statisticReport.getEndDate().substring(0, 4)+statisticReport.getWeekNo(),
						statisticReport.getSaleEarning());
			}
			
			for(String str : weekList){
				
				if(dataMap.containsKey(str)){
					dataList.add(dataMap.get(str));
				}
				else{
					dataList.add(0f);
				}
			}
		}
		
		return dataList;
	}
	
	/**
	 * 补全周的开始日期和结束日期
	 * 
	 * @param weekList
	 * @param list
	 * @return
	 */
	public List<String> getDetailForWeek(List<String> weekList){
		
		List<String> dataList = new ArrayList<String>();
		
		for(String str : weekList){
			
			Calendar c = Calendar.getInstance();
			c.setFirstDayOfWeek(Calendar.MONDAY);
			c.set(Calendar.YEAR, Integer.valueOf(str.substring(0, 4)));
			c.set(Calendar.WEEK_OF_YEAR, Integer.valueOf(str.substring(4)));
			c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
			
			Date begin = c.getTime();
			
			c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			
			Date end = c.getTime();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
			
			dataList.add("'"+sdf.format(begin) +"-"+ sdf.format(end)+"'");
		}
		
		return dataList;
	}

	@Override
	public Map<String, Object> findTotalSaleEarningByMonth() {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getMonthList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		
		// 设置当前年份和月份
		statisticReport.setYear(c.get(Calendar.YEAR));
		statisticReport.setMonthNo(c.get(Calendar.MONTH)+1);
		
		// 总数
		List<StatisticReport> totalList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		map.put("dateList", dateList);
		map.put("totalList", repairLackDataForMonth(dateList, totalList));
		map.put("pizhouList", repairLackDataForMonth(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForMonth(dateList, fuyangList));
		map.put("hushiList", repairLackDataForMonth(dateList, hushiList));
		map.put("weekDetailList", new ArrayList<String>());
		
		return map;
	}

	@Override
	public Map<String, Object> findAreaSaleEarningByMonth() {
		Map<String, Object> map = new HashMap<String, Object>();
		
		StatisticReport statisticReport = new StatisticReport();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getMonthList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());

		// 设置当前年份和月份
		statisticReport.setYear(c.get(Calendar.YEAR));
		statisticReport.setMonthNo(c.get(Calendar.MONTH)+1);
		
		// 邳州
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_PIZHOU+"%");
		List<StatisticReport> pizhouList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		// 富阳
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_FUYANG+"%");
		List<StatisticReport> fuyangList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		// 呼市
		statisticReport.setNodeCode(Globals.NODE_CODE_PREFIX_HUSHI+"%");
		List<StatisticReport> hushiList = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		map.put("dateList", dateList);
		map.put("pizhouList", repairLackDataForMonth(dateList, pizhouList));
		map.put("fuyangList", repairLackDataForMonth(dateList, fuyangList));
		map.put("hushiList", repairLackDataForMonth(dateList, hushiList));
		map.put("weekDetailList", new ArrayList<String>());

		return map;
	}

	@Override
	public Map<String, Object> findOneNodeSaleEarningByMonth(
			StatisticReport statisticReport) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		Calendar c = Calendar.getInstance();
		// 结束日期
		statisticReport.setSearchEndDate(c.getTime());
		
		// 开始日期
		statisticReport.setSearchBeginDate(getBeginDay(c));
		
		// 周集合
		List<String> dateList = getMonthList(statisticReport.getSearchBeginDate(), statisticReport.getSearchEndDate());
		
		// 设置当前年份和月份
		statisticReport.setYear(c.get(Calendar.YEAR));
		statisticReport.setMonthNo(c.get(Calendar.MONTH)+1);
		
		// 某一门店的数据
		List<StatisticReport> list = statisticReportDao.findSaleEarningByMonth(statisticReport);
		
		map.put("dateList", dateList);
		map.put("list", repairLackDataForMonth(dateList, list));
		map.put("nodeInfo", nodeInfoDao.findNodeByCode(statisticReport.getNodeCode()));
		map.put("weekDetailList", new ArrayList<String>());
		
		return map;
	}
	
	/**
	 * 根据开始和结束日期获取时间段中所月份的集合
	 * 
	 * @param begin
	 * @param end
	 * @return
	 */
	public List<String> getMonthList(Date begin,Date end){
		
		Calendar c = Calendar.getInstance();
		c.setTime(begin);
		
		List<String> list = new ArrayList<String>();
		
		Calendar ce = Calendar.getInstance();
		ce.setTime(end);
		ce.set(Calendar.DAY_OF_MONTH,1);
		
		while(c.getTime().before(ce.getTime())){
			
			list.add(c.get(Calendar.YEAR) + "" + (c.get(Calendar.MONTH)+1));
			
			c.add(Calendar.MONTH, 1);
		}	
		
		return list;
	}

	/**
	 * 用0补全没有的数据
	 * 
	 * @param dateList
	 * @param list
	 * @return
	 */
	public List<Float> repairLackDataForMonth(List<String> dateList,List<StatisticReport> list){
		
		List<Float> dataList = new ArrayList<Float>();
		
		// 数据完整，不用补全
		if(dateList.size() == list.size()){
			
			for(StatisticReport statisticReport : list){
				
				dataList.add(statisticReport.getSaleEarning());
			}
		}
		// 数据不全，用0补全
		else{
			
			Map<String, Float> dataMap = new HashMap<String, Float>();
			
			for(StatisticReport statisticReport : list){
				dataMap.put(statisticReport.getYear()+""+statisticReport.getMonthNo(),
						statisticReport.getSaleEarning());
			}
			
			for(String str : dateList){
				
				if(dataMap.containsKey(str)){
					dataList.add(dataMap.get(str));
				}
				else{
					dataList.add(0f);
				}
			}
		}
		
		return dataList;
	}
	
	/**
	 * 获取开始日期
	 * 
	 * @param c
	 * @return
	 */
	public Date getBeginDay(Calendar c){
		
		// 年份减1
		c.add(Calendar.YEAR, -1);
		
		return c.getTime();
	} 
}
